﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using GauntletFramework.Input;
using GauntletFramework.StateManagment;
using GauntletFramework.StateManagment.Loading;

namespace GauntletFramework.Games.TestGame2
{
    public class Test2 : State
    {
        public Test2()
        {
        }

        public override PhoneInputState DefualtPhoneInputState { get { return PhoneInputState.PhoneControlBottom; } }
        public override GameOrientation GameOrientation { get { return GameOrientation.Vertical; } }

        Vector2 Pos = new Vector2(100, 100);
        Color shade = Color.DeepPink;

        Vector2 EnemyPos = new Vector2(300, 100);
        Color EnemyColor = Color.Aqua;
        const int ENEMY_DISTANCE = 25;

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            
            BasicInput bi = StaticGame.BasicInput;
            if (bi.ArrowUp == AdvancedButtonState.Down)
                Pos.Y -= 100.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (bi.ArrowDown == AdvancedButtonState.Down)
                Pos.Y += 100.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (bi.ArrowLeft == AdvancedButtonState.Down)
                Pos.X -= 100.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (bi.ArrowRight == AdvancedButtonState.Down)
                Pos.X += 100.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            
            if (bi.Action1 == AdvancedButtonState.Released)
                shade = Color.Aqua;
            if (bi.Action2 == AdvancedButtonState.Released)
                shade = Color.GreenYellow;
            if (bi.Action3 == AdvancedButtonState.Released)
            {
                bi.PhoneInputState++;
                if (bi.PhoneInputState > PhoneInputState.PhoneControlPauseBottom)
                    bi.PhoneInputState = PhoneInputState.PhoneControlNone;
            }

            #region Enemy Square Following Part
            //Enemy movement, will move until it is 10, 10 away from you

            //X movement
            int xDiff = (int)(Pos.X - EnemyPos.X);
            if (xDiff > ENEMY_DISTANCE)
            {
                //enemy is to the left of us
                EnemyPos.X += 100.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (xDiff < -ENEMY_DISTANCE)
            {
                //enemy is to the right of us
                EnemyPos.X -= 100.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            //Y movement
            int yDiff = (int)(Pos.Y - EnemyPos.Y);
            if (yDiff > ENEMY_DISTANCE)
            {
                //enemy is above us
                EnemyPos.Y += 100.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (yDiff < -ENEMY_DISTANCE)
            {
                //enemy is below us
                EnemyPos.Y -= 100.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            #endregion


            if (bi.ActionPause == AdvancedButtonState.Released)
                StaticGame.StateStack.PushState(new StatePause(this));
        }

        public override void Draw(GameTime gameTime)
        {
            base.Update(gameTime);
            
            SpriteBatch sb = StaticGame.SpriteBatch;
            sb.Draw(StaticGame.Game.MonoColor, new Rectangle((int)Pos.X - 10, (int)Pos.Y - 10, 20, 20), shade);
            sb.Draw(StaticGame.Game.MonoColor, new Rectangle((int)EnemyPos.X - 10, (int)EnemyPos.Y - 10, 20, 20), EnemyColor);
        }
    }
}
